package com.eco.fanliapp.admin.controller.web;

import com.eco.fanliapp.common.constant.NewsEnumLog;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.dao.AppNewsLogMapper;
import com.eco.fanliapp.dao.AppUserOrderMapper;
import com.eco.fanliapp.entity.AppCompany;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.entity.AppUserOrder;
import com.eco.fanliapp.service.*;
import com.eco.fanliapp.utils.BigDecimalUtils;
import com.eco.fanliapp.utils.EasyPoiStyleUtils;
import com.eco.fanliapp.utils.EasyPoiUtils;
import com.eco.fanliapp.vo.DoubleElevenUserVo;
import com.eco.fanliapp.vo.PressllOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @date 2019-11-15
 */
@RequestMapping("/web/presell")
@RestController
public class PresellOrderAddMoneyController {
    @Autowired
    private IAppUserService iAppUserService;
    @Autowired
    private IAppCompanyService iAppCompanyService;
    @Autowired
    private ITaoBaoMethodService iTaoBaoMethodService;
    @Autowired
    private ISyUserMoneyLogService syUserMoneyLogService;
    @Autowired
    private AppUserOrderMapper appUserOrderMapper;
    @Autowired
    private IAppNewsLogService iAppNewsLogService;

    /**
     * 预收订单导入计算
     *
     * @return
     */
    @RequestMapping(value = "/order/import")
    @ResponseBody
    public ServerResponse pressllOrderVoImport(HttpServletResponse response, MultipartFile file) {

        List<PressllOrderVo> listBack = new ArrayList<>();

        List<PressllOrderVo> list = EasyPoiUtils.importExcel(file, 1, 1, PressllOrderVo.class);
        if (list != null && list.size() > 0) {
            for (PressllOrderVo pressllOrderVo : list
            ) {
                //1,根据userId查询用户信息
                Long userId = pressllOrderVo.getUserId();
                if(userId == null){
                    pressllOrderVo.setStatus("用户不存在");
                    listBack.add(pressllOrderVo);
                    continue;
                }
                //根据用户id，查询用户
                AppUser appUser = iAppUserService.selectAppUserByUserId(userId);
                if(appUser == null){
                    pressllOrderVo.setStatus("用户不存在");
                    listBack.add(pressllOrderVo);
                    continue;
                }
                AppCompany appCompany = iAppCompanyService.findtAppCompany(userId);
                if(appCompany == null){
                    pressllOrderVo.setStatus("用户所属公司为null");
                    listBack.add(pressllOrderVo);
                    continue;
                }

                //2，获取订单号，分佣，获取新的分佣
                String orderId = pressllOrderVo.getOrderId();
                BigDecimal oldCommission = pressllOrderVo.getOldCommission();
                Integer proportion = 0;
                if(appCompany != null){
                    //分佣比列
                     proportion = iTaoBaoMethodService.getCommissionRate(appCompany, appCompany.getOrderFeeScheme(), appCompany.getGeneralFeeScheme(), userId, pressllOrderVo.getTkCommission().doubleValue());
                }

                //3 计算出实际应得分佣
                BigDecimal newCommission = BigDecimalUtils.FanliMoney(proportion.toString(), pressllOrderVo.getTkCommission());
                pressllOrderVo.setNewCommission(newCommission);
                //用户应加金额
                BigDecimal money = newCommission.subtract(oldCommission);
                pressllOrderVo.setMoney(money);
                //给用户加钱

                int i = iAppUserService.updateAppUseraddbalance(userId, money);
                if (i > 0) {
                    pressllOrderVo.setStatus("成功");

                    //加钱成功，修改日志，new_log表
                    //1余额日志
                    syUserMoneyLogService.addSyUserMoneyLog(userId, appUser.getBalance(), money, "预付订单补贴佣金" + orderId, 2, 0);
                   //2，增加一条消息
                    iAppNewsLogService.newsType(userId,
                            NewsEnumLog.NEWS_AWARD_PRESSLL.getNewsType(),
                            BigDecimalUtils.Format(money), ""+money);

                    //修改订单表用户佣金
                    appUserOrderMapper.updatePressllOrder(userId, orderId, newCommission);
                } else {
                    pressllOrderVo.setStatus("失败");

                }
                //如果有上级，给上级加钱
               if (appUser.getSuperInviteCode() != null) {
                    AppUser user = iAppUserService.selectSuperUserInfoBySuperInviteCode(appUser.getSuperInviteCode());
                    if (user == null) {

                    } else {
                        pressllOrderVo.setSuperior(user.getUserId());

                        BigDecimal SuperiorNewMoney = newCommission.multiply(new BigDecimal("0.1"));
                        BigDecimal SuperiorOldMoney = oldCommission.multiply(new BigDecimal("0.1"));
                        BigDecimal SuperiorMoney = SuperiorNewMoney.subtract(SuperiorOldMoney);
                        pressllOrderVo.setSuperiorMoney(SuperiorNewMoney);
                        //给用户加钱
                        int ii = iAppUserService.updateAppUseraddbalance(user.getUserId(), SuperiorMoney);
                        if (ii > 0) {
                            //加钱成功，修改日志，new_log表
                            //1余额日志
                            syUserMoneyLogService.addSyUserMoneyLog(user.getUserId(), user.getBalance(), SuperiorMoney, "预付订单补贴佣金上级" + orderId, 2, 0);
                            //修改订单表用户佣金
                            appUserOrderMapper.updatePressllOrder(user.getUserId(), orderId, SuperiorOldMoney);
                        }
                    }
                }
                listBack.add(pressllOrderVo);
            }
        }

        EasyPoiUtils.exportExcel(listBack, "预售订单", "预售订单", PressllOrderVo.class, EasyPoiStyleUtils.class, "pressllOrder.xls", response);
        return null;
    }
}
